import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  // 首页
  {
    path: '/',
    component: Layout,
    // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
    redirect: '/home',
    children: [{
      path: '/home',
      name: 'Home',
      component: () => import('@/views/home/index'),
      meta: { title: '总览', icon: 'dashboard' }
    }]
  },

  // 用户管理
  {
    path: '/userManage',
    component: Layout,
    redirect: '/userManage',
    children: [{
      path: '/userManage',
      name: 'UserManage',
      component: () => import('@/views/userManage/index'),
      meta: { title: '用户管理', icon: 'user' }
    }]
  },

  // 币价行情
  {
    path: '/priceTrend',
    component: Layout,
    redirect: '/priceTrend',
    children: [{
      path: '/priceTrend',
      name: 'PriceTrend',
      component: () => import('@/views/priceTrend/index'),
      meta: { title: '币价行情', icon: 'el-icon-s-marketing' }
    }]
  },

  // 信用度管理
  {
    path: '/creditManage',
    component: Layout,
    redirect: '/creditManage',
    children: [{
      path: '/creditManage',
      name: 'CreditManage',
      component: () => import('@/views/creditManage/index'),
      meta: { title: '信用度管理', icon: 'el-icon-star-on' }
    }]
  },

  // 音值管理
  {
    path: '/valueManage',
    component: Layout,
    redirect: '/valueManage',
    children: [{
      path: '/valueManage',
      name: 'ValueManage',
      component: () => import('@/views/valueManage/index'),
      meta: { title: '音值管理', icon: 'el-icon-coin' }
    }]
  },
  // 委托订单
  {
    path: '/entrustOrder',
    component: Layout,
    redirect: '/entrustOrder',
    children: [{
      path: '/entrustOrder',
      name: 'EntrustOrder',
      component: () => import('@/views/entrustOrder/index'),
      meta: { title: '委托订单', icon: 'form' }
    }]
  },
  // 交易订单
  {
    path: '/transactionOrder',
    component: Layout,
    redirect: '/transactionOrder',
    children: [{
      path: '/transactionOrder',
      name: 'TransactionOrder',
      component: () => import('@/views/transactionOrder/index'),
      meta: { title: '交易订单', icon: 'el-icon-s-order' }
    }]
  },
  // 申诉管理
  {
    path: '/appealManage',
    component: Layout,
    redirect: '/appealManage',
    children: [{
      path: '/appealManage',
      name: 'AppealManage',
      component: () => import('@/views/appealManage/index'),
      meta: { title: '申诉管理', icon: 'el-icon-chat-line-square' }
    }]
  },

  // 管理员设置
  {
    path: '/adminSetting',
    component: Layout,
    redirect: '/adminSetting/adminManage',
    name: 'AdminSetting',
    meta: { title: '管理员设置', icon: 'el-icon-s-tools' },
    children: [
      {
        path: 'adminManage',
        name: 'AdminManage',
        component: () => import('@/views/adminSetting/adminManage/index'),
        meta: { title: '管理员管理', icon: 'el-icon-s-custom' }
      },
      {
        path: 'authManage',
        name: 'AuthManage',
        component: () => import('@/views/adminSetting/authManage/index'),
        meta: { title: '授权管理', icon: 'el-icon-set-up' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
